package com.quanyan.user.service.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.Page;
import com.quanyan.user.mapper.TbBannerUpdateConfigMapper;
import com.quanyan.user.mapper.TbBannerUpdateMapper;
import com.quanyan.user.mapper.TbBannerUserPushMapper;
import com.quanyan.user.model.db.TbBannerUpdate;
import com.quanyan.user.model.db.TbBannerUpdateConfig;
import com.quanyan.user.model.db.TbBannerUpdateConfigExample;
import com.quanyan.user.model.db.TbBannerUpdateExample;
import com.quanyan.user.model.vo.response.RespChannelInfo;
import com.quanyan.user.service.DownloadChannelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by slong.guo on 2017/5/5.
 */

@Service
public class DownloadChannelServiceImpl implements DownloadChannelService {

    private static final Logger logger = LoggerFactory.getLogger(DownloadChannelServiceImpl.class);

    @Autowired
    private TbBannerUpdateMapper bannerUpdateMapper;

    @Autowired
    private TbBannerUpdateConfigMapper bannerUpdateConfigMapper;

    @Autowired
    private TbBannerUserPushMapper bannerUserPushMapper;


    @Override
    public APIResponse getChannelInfo(int uid, int cityId, int devType) {

        if (devType != 1 && devType != 2) {
            return APIResponse.returnFail("无效的系统类型");
        }
        logger.info("getChannelInfo params: uid:{} cityId:{} devType:{}", uid, cityId, devType);


//        TbBannerUserPushExample exampleUser = new TbBannerUserPushExample();
//        TbBannerUserPushExample.Criteria criteriaUser = exampleUser.createCriteria();
//
//        criteriaUser.andUidEqualTo(uid);
//        criteriaUser.andIsDeleteEqualTo((byte)0);
//
//        List<TbBannerUserPush> userPushes = bannerUserPushMapper.selectByExample(exampleUser);
//
//        if(userPushes == null || userPushes.isEmpty()){
//            return APIResponse.returnSuccess(getInvalidResp());
//        }
//
//        Integer appType = userPushes.get(0).getAppType();
//        if(appType == null || appType.compareTo(0) < 0){
//            return APIResponse.returnSuccess(getInvalidResp());
//        }
//
//        if(appType.equals(0)){
//            appType = 1 + (int)(Math.random() * 2);
//        }

        TbBannerUpdateExample example = new TbBannerUpdateExample();
        TbBannerUpdateExample.Criteria criteria = example.createCriteria();
        criteria.andCityIdEqualTo(cityId);
        criteria.andOsTypeEqualTo((byte) devType);
        criteria.andIsDeleteEqualTo((byte) 0);
//        criteria.andAppTypeEqualTo(appType);

        List<TbBannerUpdate> bannerUpdates = bannerUpdateMapper.selectByExampleWithBLOBs(example);
        if (bannerUpdates.isEmpty()) {
            return APIResponse.returnSuccess(getInvalidResp());
        }

        RespChannelInfo chanInfo = new RespChannelInfo();
        TbBannerUpdate bannerUpdate = bannerUpdates.get(0);
        if (bannerUpdates.size() > 1) {
            bannerUpdate = bannerUpdates.get((int) (Math.random() * bannerUpdates.size()));
        }


        Map<String, String> kvs = getChannelConfigs();
        Integer androidUpdate = 0;
        Integer iosUpdate = 0;
        if (kvs.containsKey("androidUpdate")) {
            androidUpdate = Integer.parseInt(kvs.get("androidUpdate"));
        }
        if (kvs.containsKey("iosUpdate")) {
            iosUpdate = Integer.parseInt(kvs.get("iosUpdate"));
        }

        if (devType == 1) {
            chanInfo.setAndroidUpdate(androidUpdate);
            chanInfo.setAndroidImageUrl(bannerUpdate.getBackImg());
            chanInfo.setAndroidUrl(bannerUpdate.getUrl());
            chanInfo.setAndroidPkgName(bannerUpdate.getPkgName());

            String markets = bannerUpdate.getMarkets();
            if (markets != null && !markets.isEmpty()) {
                chanInfo.setMarketList(markets.split(","));
            }

        } else {
            chanInfo.setIosUpdate(iosUpdate);
            chanInfo.setIosUrl(bannerUpdate.getUrl());
            chanInfo.setIosImageUrl(bannerUpdate.getBackImg());
        }


        return APIResponse.returnSuccess(chanInfo);
    }

    private Map<String, String> getChannelConfigs() {
        TbBannerUpdateConfigExample example = new TbBannerUpdateConfigExample();
        TbBannerUpdateConfigExample.Criteria criteria = example.createCriteria();

        example.setPage(new Page(0, 100));

        List<TbBannerUpdateConfig> updateConfigs = bannerUpdateConfigMapper.selectByExample(example);
        HashMap<String, String> kvs = new HashMap<>();
        for (TbBannerUpdateConfig updateConfig : updateConfigs) {
            kvs.put(updateConfig.getConfigName(), updateConfig.getConfigVals());
        }
        return kvs;
    }

    private RespChannelInfo getInvalidResp() {
        RespChannelInfo channelInfo = new RespChannelInfo();
        channelInfo.setIosUpdate(0);
        channelInfo.setAndroidUpdate(0);
        return channelInfo;
    }
}
